package guoanmaker.operator.business.dao;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import guoanmaker.operator.business.modelnobind.OperatorActiveTaskVO;
import guoanmaker.operator.business.modelnobind.OperatorEventDetailsOptions;

/**
 * @author ws
 * @date 2017-11-23
 */

@Repository
public class OperatorActiveTaskDao {

	@PersistenceContext
	EntityManager em;
	
	/**
	 * @description: 从表t_operator_event_details中根据luck_name and activity_type_id  and availability查询记录
	 * @param availability
	 * @param activityTypeId
	 * @param luckName
	 * @return List<OperatorEventDetailsOptions>
	 */
	public List<OperatorEventDetailsOptions> findOperatorEventDetailsByAvailabilityAndActivityTypeIdAndLuckName(String availability, String activityTypeId, String luckName){
		String jqpl = "select new guoanmaker.operator.business.modelnobind.OperatorEventDetailsOptions(o.id, o.eventTitle) from OperatorEventDetails o " 
					+"where (o.state=1 or o.state = 2) and o.availability=:availability and o.activityTypeId=:activityTypeId and (o.luckName='大转盘' or o.luckName='砸金蛋')";
		Query query = em.createQuery(jqpl);
		query.setParameter("availability", Integer.parseInt(availability));
		query.setParameter("activityTypeId", activityTypeId);
		//query.setParameter("luckName", luckName);
		
		List<OperatorEventDetailsOptions> queryResult = query.getResultList();
		
		if(queryResult == null || queryResult.size() == 0){
			return null;
		}
		
		return queryResult;
	}
	
	/**
	 * @description 查询同一任务类型下，同名任务的任务数量
	 * @param taskId 任务id
	 * @param taskName 任务名称
	 * @param taskTypeId 任务类型id
	 * @return long
	 */
	public long countSameTaskName(String taskId, String taskName, String taskTypeId){
		String jpql = "select count(o.id) from OperatorActiveTask o left join o.activeTaskType type where o.activeTaskName=:taskName and type.id =:taskTypeId";
		if(taskId != null && taskId.trim().length() > 0){
			jpql += " and o.id <> :taskId";
		}

		Query query = em.createQuery(jpql);
		query.setParameter("taskName", taskName);
		query.setParameter("taskTypeId", taskTypeId);
		if(taskId != null && taskId.trim().length() > 0){
			query.setParameter("taskId", taskId);
		}
		
		return (long)query.getSingleResult();
	}
	
	/**
	 * @description 查询同一任务类型下，订单金额相同的任务数量
	 * @param taskId 任务id
	 * @param taskAmount 订单金额
	 * @param taskTypeId 任务类型id
	 * @return long
	 */
	public long countSameTaskAmount(String taskId, String taskAmount, String taskTypeId){
		String jpql = "select count(o.id) from OperatorActiveTask o left join o.activeTaskType type where o.activeTaskAmount=:taskAmount and type.id=:taskTypeId";
		if(taskId != null && taskId.trim().length() > 0){
			jpql += " and o.id <> :taskId";
		}
		
		Query query = em.createQuery(jpql);
		query.setParameter("taskAmount", taskAmount);
		query.setParameter("taskTypeId", taskTypeId);
		if(taskId != null && taskId.trim().length() > 0){
			query.setParameter("taskId", taskId);
		}
		
		return (long)query.getSingleResult();
	}
	
	/**
	 * @description:根据活动名称和活动类型分页查询
	 * @param taskName 任务名称
	 * @param taskTypeId 任务类型id
	 * @param page 当前页  从0开始
	 * @param size 页面可显示记录数量
	 * @return List<OperatorActiveTaskVO>
	 */
	public List<OperatorActiveTaskVO> findByTaskNameAndTaskType(String taskName, String taskTypeId, String page, String size){
		
		String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorActiveTaskVO("
						+" task.id, task.activeTaskName, task.activeTaskAmount, type.activeTaskTypeName, type.id, active.eventTitle, active.id, active.luckName)" 
						+ " from OperatorActiveTask task left join task.activeTaskType type left join task.active active" 
						+ " where 1=1";
		if(taskTypeId != null && taskTypeId.trim().length() != 0){
			jpql += " and type.id=:taskTypeId";
		}
		if(taskName != null && taskName.trim().length() != 0){
			jpql += " and task.activeTaskName like :taskName";
		}
		jpql += "order by task.dateTime DESC";
		
		
		Query query = em.createQuery(jpql);
		if(taskTypeId != null && taskTypeId.trim().length() != 0){
			query.setParameter("taskTypeId", taskTypeId);
		}
		if(taskName != null && taskName.trim().length() != 0){
			query.setParameter("taskName", taskName+"%");
		}

		int page1 = Integer.parseInt(page);
		int size1 = Integer.parseInt(size);
		query.setFirstResult(page1 * size1);
		query.setMaxResults(size1);
		
		List<OperatorActiveTaskVO> resultList = query.getResultList();
		
		return resultList;
	}
	
	/**
	 * @description: 根据活动名称和活动类型查询总数
	 * @param taskName 任务名称
	 * @param taskTypeId 任务类型id
	 */
	public long totalByTaskNameAndTaskType(String taskName, String taskTypeId){
		String jpql = "select count(task.id)"
				+ " from OperatorActiveTask task left join task.activeTaskType type left join task.active active" 
				+ " where 1=1";
		if(taskTypeId != null && taskTypeId.trim().length() != 0){
			jpql += " and type.id=:taskTypeId";
		}
		if(taskName != null && taskName.trim().length() != 0){
			jpql += " and task.activeTaskName like :taskName";
		}
		
		Query query = em.createQuery(jpql);
		if(taskTypeId != null && taskTypeId.trim().length() != 0){
			query.setParameter("taskTypeId", taskTypeId);
		}
		if(taskName != null && taskName.trim().length() != 0){
			query.setParameter("taskName", taskName+"%");
		}
		
		return (long)query.getSingleResult();
	}
	
	
	/**
	 * @description:根据订单中的实付金额-优惠金额和任务类型id查询对应的活动id和活动类型
	 * @param provideOrderFormId 订单id
	 * @param taskTypeId 任务类型id
	 * @return Map<String, String> :key:id/luckName value:活动id/活动类型(luckName)
	 */
	public Map<String, String> findActiveIdAndLuckNameByTruePriceAndTaskTypeId(String provideOrderFormId, String taskTypeId){
		String jpql = "select active.id, active.luckName from OperatorActiveTask task left join task.active active" 
					+ " where task.activeTaskType.id=:taskTypeId and task.activeTaskAmount <= :spendMoney"
					+ " order by task.activeTaskAmount DESC";
		
		//取得truePrice
		String jpql0 = "select p.truePrice from ProvideOrderForm p where p.id=:provideOrderFormId";
		Query query0 = em.createQuery(jpql0);
		query0.setParameter("provideOrderFormId", provideOrderFormId);
		BigDecimal truePrice = BigDecimal.valueOf((double)query0.getSingleResult());
		truePrice = truePrice==null?BigDecimal.ZERO:truePrice;
		
		//取得preferentialMoney优惠金额
		String jpql1 = "select p.preferentialMoney from ProvideOrderForm p where p.id=:provideOrderFormId";
		Query query1 = em.createQuery(jpql1);
		query1.setParameter("provideOrderFormId", provideOrderFormId);
		BigDecimal preferentialMoney = BigDecimal.valueOf((double)query1.getSingleResult());
		preferentialMoney = preferentialMoney==null?BigDecimal.ZERO:preferentialMoney;
		
		//获得spendMoney
		BigDecimal spendMoney = truePrice.subtract(preferentialMoney);
		
		Query query = em.createQuery(jpql);
		query.setParameter("taskTypeId", taskTypeId);
		query.setParameter("spendMoney", (spendMoney==null?BigDecimal.ZERO:spendMoney).toString());
		query.setFirstResult(0);
		query.setMaxResults(1);
		List<Object[]> resultList = query.getResultList();
		
		Map<String, String> resultMap = new HashMap<String, String>(); 
		if(resultList != null && resultList.size() != 0){
			resultMap.put("id", (String)resultList.get(0)[0]);
			resultMap.put("luckName", (String)resultList.get(0)[1]);
			return resultMap;
		}
		
		return null;
	}
	
}
